<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\ApiController;
use App\Http\Resources\Role\RoleResource;
use App\Services\Role\RoleService;
use Illuminate\Http\Request;

/**
 * Class RoleController
 * @package App\Http\Controllers\Admin
 */
class RoleController extends ApiController
{

    /**
     * @var RoleService
     */
    protected $roleService;

    /**
     * RoleController constructor.
     * @param RoleService $roleService
     */
    public function __construct(RoleService $roleService)
    {
        $this->roleService = $roleService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $roles = $this->roleService->findRoles();

        return $this->success(RoleResource::collection($roles));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = [
            'name'  =>  $request->get('name'),
            'guard_name'    =>  $request->get('guard_name'),
        ];

        try{
            $this->roleService->insertRole($data);

            $roles = $this->roleService->findRoles();

            return $this->success(RoleResource::collection($roles));
        }catch (\Exception $exception) {
            return $this->failed('添加失败', 500);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        try{
            $role = $this->roleService->findRoleById($id);

            return $this->success(new RoleResource($role));
        }catch (\Exception $exception) {
            return $this->notFond('请求错误');
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        try{
            $role = $this->roleService->findRoleById($id);

            return $this->success(new RoleResource($role));
        }catch (\Exception $exception) {
            return $this->notFond('请求错误');
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = [
            'name'  =>  $request->get('name'),
            'guard_name'    =>  $request->get('guard_name'),
        ];

        try{
            $this->roleService->updateRoleById($data, $id);

            $roles = $this->roleService->findRoles();

            return $this->success(RoleResource::collection($roles));
        }catch (\Exception $exception) {
            return $this->failed('编辑失败', 500);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        try{
            $this->roleService->deleteRoleById($id);

            $roles = $this->roleService->findRoles();

            return $this->success(RoleResource::collection($roles));
        }catch (\Exception $exception) {
            return $this->failed('删除失败', 500);
        }
    }
}
